<?php

/**

 *
 * @author lovebing <lovebing365@126.com> Created on 2014-9-21 0:06:44
 */
class BaseController extends CController {

    public function init() {
        Yii::app()->attachEventHandler('onException', array($this, 'exceptionEvent'));
        Yii::app()->attachEventHandler('onError', array($this, 'exceptionError'));
    }

    /**
     * 
     * @param CEvent $event
     */
    protected function exceptionEvent(CEvent $event) {
        LogUtil::exception($event->exception);
        if (!is_a($event->exception, 'RuntimeException')) {
            
        }

        if (method_exists($event->exception, 'handle')) {
            // $event->exception->handle();
        }


        $this->handleException($event->exception);
    }

    /**
     * 
     * @param CErrorEvent $e
     */
    protected function exceptionError(CErrorEvent $e) {
        LogUtil::error($e);
    }

    /**
     * 
     * @param Exception $ex
     */
    protected function handleException(Exception $ex) {
        if (Yii::app()->request->isAjaxRequest) {
            $this->outputJSON($ex->getMessage(), array(), 1);
            Yii::app()->end();
        } else {
            $this->exceptionPageHandle($ex);
        }
    }

    /**
     * 
     * @param Exception $ex
     */
    public function exceptionPageHandle(Exception $ex) {
        switch ($ex->getCode()) {
            case 403:
            case 404:
                header('HTTP/1.1 ' . $ex->getCode() . ' Error');
                echo $ex->getMessage();
                break;
            default :
                echo $ex->getMessage();
        }
        Yii::app()->end();
    }

    /**
     * 
     * @param array $data
     * @param string $msg
     * @param int $code
     */
    public function outputJSON($msg, array $data, $code = 0) {
        $callback = filter_input(INPUT_GET, 'callback');
        $output = array(
            'data' => $data,
            'msg' => $msg,
            'code' => intval($code)
        );
        $text = json_encode($output);
        if ($callback) {
            header('Content-type: text/javascript');
            $text = ';try {' . $callback . '(' . $text . ');} catch(e){};';
        } else {
            header('Content-type: application/json');
        }
        echo $text;
        Yii::app()->end();
    }

    /**
     * 
     * @param array $data
     */
    public function outputXML(array $data) {
        header('Content-type: text/xml');
        echo XML::arrayToXml($data);
        Yii::app()->end();
    }

    /**
     * 
     * @param array $errors
     */
    public function invalidFormInput(array $errors) {
        $this->outputJSON('', $errors, -111);
    }

    /**
     * 
     * @param string $msg
     * @param array $data
     */
    public function success($msg, array $data = array()) {
        $this->outputJSON($msg, $data);
    }

    /**
     * 
     * @param array $data
     * @return type
     */
    public function cActiveRecordListToAssocArrayList($data) {
        if (empty($data)) {
            return array();
        }
        $res = array();
        foreach ($data as $v) {
            $res[] = $v->getAttributes();
        }
        return $res;
    }

    public function getCurrentUrl() {
        $url = Yii::app()->request->getHostInfo() . Yii::app()->request->getUrl();
        return str_replace('index.php/', '', $url);
    }

    /**
     * 
     * @param type $str
     * @return type
     */
    public function parseCustomParams($str) {
        $arr = explode('-', $str);
        $res = array();
        $len = count($arr);
        for ($i = 0; $i < $len; $i+=2) {
            if(trim($arr[$i]) == '') {
                continue;
            }
            $val = isset($arr[$i + 1]) ? $arr[$i + 1] : '';
            $res[$arr[$i]] = $val;
        }
        return $res;
    }

    /**
     * 
     * @param type $data
     * @return type
     */
    public function createCustomParams($data) {
        $res = array();
        foreach($data as $k=>$v) {
            $res[] = $k.'-'.$v;
        }
        return implode('-', $res);
    }
    
    public function getCustomParams() {
       return isset($_GET['params']) ? $_GET['params'] : '';
    }
    
    /*
     * 获取子域名
     */
    public function getMobileDomain($wid) {
        $pub = Pubs::getModelById($wid);

        $domain = 'm.xiaowangtong.com.cn';
        if($pub->custom_domain_enable_flag) {
            $domain = $wid.'.'.$domain;
        }
        return $domain;
   }
}
